require 'helper_common'
require 'helper_watir'
require 'helper_autoit'
require 'testcase'
require 'value_processor'

module LWA
  class Engine

    attr_reader :test_case
    attr_accessor :close_browser_when_finished

    def initialize
      @auto_hash = Hash.new
      @value_processor = ValueProcessor.new("..\\lib\\value_storage.yaml")
    end

    def process_single_step(one_step)
      begin
        automator = set_automator(one_step.by)
        automator.send(one_step.command, @value_processor.get_value(one_step.target), @value_processor.get_value(one_step.value))
      rescue => error
        raise RuntimeError, "In step #{one_step.id}, #{error.to_s}", caller
      end
    end

    def process_testcase(obj_testcase)
      step = nil
      load_testcase(obj_testcase)
      begin
        @test_case.step_list.each do |step_x|
          step = step_x
          process_single_step(step_x)
        end
      rescue => err
        puts "ERROR: In step #{step.id}, #{err}"
        raise err
      ensure
        @auto_hash.clear
        kill_process("iexplore.exe") if @close_browser_when_finished
      end
    end

    private

    def set_automator(automator)
      class_name, instance_name, init_parameter = automator.split(".")
      key = (instance_name == nil) ? class_name : class_name + instance_name
      if @auto_hash.has_key?(key)
        @auto_hash[key]
      else
        autor = Object.const_get(class_name).new(init_parameter)
        @auto_hash.store(key, autor)
        return autor
      end
    end

    def load_testcase(obj_testcase)
      @test_case = obj_testcase
    end
  end
end